/**
 * app.getPath方法参数传值
 userData 储存你应用程序设置文件的文件夹，默认是 appData 文件夹附加应用的名称
 temp 临时文件夹
 exe当前的可执行文件
 module The libchromiumcontent 库
 desktop 当前用户的桌面文件夹
 documents 用户文档目录的路径
 downloads 用户下载目录的路径
 music 用户音乐目录的路径
 pictures 用户图片目录的路径
 videos 用户视频目录的路径
 recent 用户最近文件的目录（仅Windows）。
 logs应用程序的日志文件夹
 crashDumps 故障转储的存储目录。
 */
// eslint-disable-next-line no-unused-vars
//import da from 'element-ui/src/locale/lang/da'
// eslint-disable-next-line no-unused-vars
import DateUtil from '@/utils/DateUtil'

const fs = require('fs')

function FileSystem () {
  const { app } = require('electron').remote
  const thiz = this
  this.fs = require('fs')
  this.path = require('path')
  /**
   * 获取安装目录
   * @returns {string}
   */
  this.getInstallPath = function () {
    return this.path.dirname(app.getPath('exe'))
  }
  /**
   * 获取用户数据目录
   * @returns {string}
   */
  this.getUserDataPath = function () {
    return app.getPath('userData')
  }
  /**
   * 获取文件或目录信息
   * @param path
   */
  this.getFileStat = function (path) {
    if (!this.isExists(path)) {
      console.log(path + ' is not exists')
      return
    }
    return this.fs.statSync(path)
  }
  /**
   * 文件HiFi存在
   * @param path
   * @returns {boolean}
   */
  this.isExists = function (path) {
    return this.fs.existsSync(path)
  }
  /**
   * 是否是文件
   * @param path
   * @returns {boolean}
   */
  this.isFile = function (path) {
    if (!this.isExists(path)) {
      return false
    }
    let statObj = this.getFileStat(path)
    if (!statObj) {
      return false
    }
    return statObj.isFile()
  }
  /**
   * 是否文件目录
   * @param path
   */
  this.isDirectory = function (path) {
    if (!this.isExists(path)) {
      return false
    }
    let statObj = this.getFileStat(path)
    if (!statObj) {
      return false
    }
    return statObj.isDirectory()
  }
  /**
   * 获取目录下的所有目录
   * @param path
   */
  this.getFiles = function (path, callback, callbackObject) {
    this.fs.readdir(path, function (err, files) {
      if (err) {
        console.log('getFiles error!', err)
        thiz.tipMessage(err, callbackObject)
        callback && callback.call(this, [])
        return
      }
      callback && callback.call(this, files)
    })
  }
  /**
   * 获取目录下文件夹
   * @param path
   * @param callback
   * @param callbackObject
   */
  this.getFolders = function (path, callback, callbackObject) {
    this.fs.readdir(path, function (err, files) {
      if (err) {
        console.log('getFolders error!', err)
        thiz.tipMessage(err, callbackObject)
        callback && callback.call(callbackObject || this, [])
        return
      }
      let folders = []
      for (let i = 0; i < files.length; i++) {
        let fileStat = thiz.getFileStat(path + '\\' + files[i])
        if (fileStat && fileStat.isDirectory()) {
          folders.push({
            name: files[i],
            path: path + '\\' + files[i],
            date: DateUtil.dateFormat(fileStat.mtime)
          })
        }
      }
      callback && callback.call(callbackObject || this, folders)
    })
  }
  /**
   * 同步获取目录下的所有目录
   * @param path
   */
  this.getFilesSync = function (path) {
    if (!this.isDirectory(path)) {
      return []
    }
    var files = this.fs.readdirSync(path)
    return files || []
  }
  /**
   * 创建目录
   * @param dirname
   */
  this.makeAllDirectory = function (dirname) {
    if (this.fs.existsSync(dirname)) {
      return true
    } else {
      if (this.makeAllDirectory(this.path.dirname(dirname))) {
        this.fs.mkdirSync(dirname)
        return true
      }
    }
  }
  /**
   * 创建目录
   * @param path
   * @param callback
   */
  this.makeDirectory = function (path, callback, callbackObject) {
    this.fs.mkdir(path, function (err) {
      if (err) {
        thiz.tipMessage(err, callbackObject)
      }
      callback && callback.call(this, !err)
    })
  }
  /**
   * 同步创建目录
   * @param path
   * @param callback
   */
  this.makeDirectorySync = function (path) {
    try {
      if(this.isExists(path)){
        return true;
      }
      this.fs.mkdirSync(path)
      return true
    } catch (e) {
      console.log('makeDirectorySync error', e)
    }
    return false
  }
  /**
   * 删除目录
   * @param path
   * @param callback
   */
  this.removeDirectory = function (path, callback, callbackObject) {
    this.fs.rmdir(path, function (err) {
      if (err) {
        thiz.tipMessage(err, callbackObject)
      }
      callback && callback.call(this, !err)
    })
  }
  /**
   * 同步删除目录
   * @param path
   * @returns {boolean}
   */
  this.removeDirectorySync = function (path) {
    try {
      this.fs.rmdirSync(path)
      return true
    } catch (e) {
      console.log('removeDirectorySync', e)
    }
    return false
  }
  /**
   * 读取文件
   * @param filepath
   * @param callback
   */
  this.readFile = function (filepath, callback, callbackObject) {
    if (!this.isFile(filepath)) {
      callback && callback.call(this)
      return
    }
    this.fs.readFile(filepath, 'utf-8', function (err, data) {
      if (err) {
        console.log('readFile error', err)
        thiz.tipMessage(err, callbackObject)
        callback && callback.call(this)
        return
      }
      callback && callback.call(this, data)
    })
  }
  /**
   * 异步读取文件
   * @param filepath
   */
  this.readFileSync = function (filepath) {
    if (!this.isFile(filepath)) {
      return
    }
    try {
      var data = this.fs.readFileSync(filepath, 'utf-8')
      return data
    } catch (e) {
      console.log('readFileSync error', e)
    }
  }
  /**
   * 写文件
   * @param filepath
   * @param data
   * @param callback
   */
  this.writeFile = function (filepath, data, callback, callbackObject) {
    this.fs.writeFile(filepath, data, function (err) {
      if (err) {
        console.log('writeFile error!', err)
        thiz.tipMessage(err, callbackObject)
        callback && callback.call(this, false)
      } else {
        callback && callback.call(this, true)
      }
    })
  }
  /**
   * 同步写文件
   * @param filepath
   * @param data
   * @returns {boolean}
   */
  this.writeFileSync = function (filepath, data) {
    try {
      this.fs.writeFileSync(filepath, data)
      return true
    } catch (e) {
      console.log('writeFileSync error!', e)
    }
    return false
  }
  /**
   * 提示错误信息
   * @param msg
   */
  this.tipMessage = function (msg, dataCallbackObject) {
    dataCallbackObject && dataCallbackObject.$message(msg)
  }
  /**
   * 复制文件
   * @param sourceFile
   * @param destFile
   */
  this.copyFile = function (source, destination, callback) {
    const readStream = fs.createReadStream(source);
    const writeStream = fs.createWriteStream(destination);

    readStream.on('error', (error) => callback(error, null));
    writeStream.on('error', (error) => callback(error, null));
    writeStream.on('close', () => callback(null, destination));

    readStream.pipe(writeStream);
  }
}

export default new FileSystem()
